import { reactive, markRaw } from 'vue'
import { removeUndefinedParams } from '@/utils/index'
import type { CTableProps } from '@/components/CTable'
import type { BasicPagingListParams } from '@/apis/basicModel'
import type { SearchOptionsType, TableFieldsSettingType } from '@/components/CPageTable'
import { ColumnSettingTypeEnum } from '@/components/CPageTable'
import { getSupplierList } from '@/apis/system/config/supplier'

export const useSupplierList = () => {
  const searchForm = reactive({
    nameOrCode: undefined,
    supplierType: undefined
  })
  const searchOptions: SearchOptionsType[] = markRaw([
    {
      name: '供应商编码或名称',
      placeholder: '供应商编码或名称',
      field: 'nameOrCode',
      type: 'input'
    },
    {
      name: '供应商类型',
      placeholder: '供应商类型',
      field: 'supplierType',
      type: 'select',
      fixedOption: 'SupplierTypeOptions'
    }
  ])

  // 表格配置
  const tableOptions: CTableProps = {
    loadData
  }

  function loadData(parameter: BasicPagingListParams) {
    const params = Object.assign(handleParams(), parameter)
    return getSupplierList(params)
  }

  // 处理参数
  const handleParams = () => {
    // 参数
    let params = {
      nameOrCode: searchForm?.nameOrCode,
      supplierType: searchForm?.supplierType
    }
    params = removeUndefinedParams(params)
    return params
  }

  const tableFieldsSetting: TableFieldsSettingType[] = [
    {
      title: '供应商名称/编码',
      field: 'nameOrCode',
      slot: 'supplierSlot',
      minWidth: 160,
      columnSettingConfig: {
        enabled: true,
        type: ColumnSettingTypeEnum.REQUIRED
      }
    },
    {
      title: '所属企业',
      field: 'enterpriseName',
      minWidth: 160,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '供应商类型',
      field: 'supplierType',
      slot: 'supplierType',
      width: 120,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '数据清单',
      field: 'formNum',
      width: 150,
      align: 'right',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '创建人/创建时间',
      field: 'creator',
      slot: 'creatorSlot',
      width: 200,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '操作',
      field: 'op',
      slot: 'op',
      width: 200,
      fixed: 'right',
      columnSettingConfig: {
        enabled: true,
        type: ColumnSettingTypeEnum.FIXED
      }
    }
  ]

  return {
    searchForm,
    searchOptions,
    tableOptions,
    tableFieldsSetting
  }
}
